﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;using NLog;

    public class DmCtDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public DataSet GetDataSet()
        {
            DataSet set2;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmCt_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSet(string ps_Code)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Ct", ps_Code) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmCt_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ten_Ct", row["Ten_Ct"]), new SqlParameter("p_Ten_Ct_E", row["Ten_Ct_E"]), new SqlParameter("p_So_Ct_Type", row["So_Ct_Type"]), new SqlParameter("p_Numbering", row["Numbering"]), new SqlParameter("p_HD_Ctrl", row["HD_Ctrl"]), new SqlParameter("p_BP_Ctrl", row["BP_Ctrl"]), new SqlParameter("p_SP_Ctrl", row["SP_Ctrl"]), new SqlParameter("p_KM_Ctrl", row["KM_Ctrl"]), new SqlParameter("p_TT_Ctrl", row["TT_Ctrl"]), new SqlParameter("p_TTe_Ctrl", row["TTe_Ctrl"]), new SqlParameter("p_Tk_Ctrl", row["Tk_Ctrl"]), new SqlParameter("p_NK_Ctrl", row["NK_Ctrl"]), new SqlParameter("p_DB_Ctrl", row["DB_Ctrl"]), new SqlParameter("p_CP_Ctrl", row["CP_Ctrl"]), new SqlParameter("p_HH_Ctrl", row["HH_Ctrl"]), 
                new SqlParameter("p_SL_Ctrl", row["SL_Ctrl"]), new SqlParameter("p_VAT_Ctrl", row["VAT_Ctrl"]), new SqlParameter("p_No_Co", row["No_Co"]), new SqlParameter("p_Tk", row["Tk"]), new SqlParameter("p_Title", row["Title"]), new SqlParameter("p_Title_E", row["Title_E"]), new SqlParameter("p_Master_Vars", row["Master_Vars"]), new SqlParameter("p_Detail_Vars", row["Detail_Vars"]), new SqlParameter("p_RepoFile", row["RepoFile"]), new SqlParameter("p_Series", row["Series"]), new SqlParameter("p_FreeFields", row["FreeFields"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x1b].Direction = ParameterDirection.Output;
            values[0x1b].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmCt_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[0].Value;
                num = (int) values[0x1b].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }
    }
}

